package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.dto.VideoListDTO;
import com.ruoyi.business.vo.VideoInfoVO;
import com.ruoyi.business.vo.VideoListVO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.VideoMapper;
import com.ruoyi.business.domain.Video;
import com.ruoyi.business.service.IVideoService;
import org.springframework.util.ObjectUtils;

/**
 * 视频Service业务层处理
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Service
public class VideoServiceImpl implements IVideoService 
{
    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询视频
     * 
     * @param id 视频主键
     * @return 视频
     */
    @Override
    public Video selectVideoById(Long id)
    {
        return videoMapper.selectVideoById(id);
    }

    /**
     * 查询视频列表
     * 
     * @param video 视频
     * @return 视频
     */
    @Override
    public List<Video> selectVideoList(Video video)
    {
        return videoMapper.selectVideoList(video);
    }

    /**
     * 新增视频
     * 
     * @param video 视频
     * @return 结果
     */
    @Override
    public int insertVideo(Video video)
    {
        video.setCreateTime(DateUtils.getNowDate());
        return videoMapper.insertVideo(video);
    }

    /**
     * 修改视频
     * 
     * @param video 视频
     * @return 结果
     */
    @Override
    public int updateVideo(Video video)
    {
        video.setUpdateTime(DateUtils.getNowDate());
        return videoMapper.updateVideo(video);
    }

    /**
     * 批量删除视频
     * 
     * @param ids 需要删除的视频主键
     * @return 结果
     */
    @Override
    public int deleteVideoByIds(Long[] ids)
    {
        return videoMapper.deleteVideoByIds(ids);
    }

    /**
     * 删除视频信息
     * 
     * @param id 视频主键
     * @return 结果
     */
    @Override
    public int deleteVideoById(Long id)
    {
        return videoMapper.deleteVideoById(id);
    }

    @Override
    public List<VideoListVO> selectVideoListVO(VideoListDTO dto) {
        Long id = tokenService.getUserId();
        dto.setEnterpriseId(id);
        return videoMapper.selectVideoListVO(dto);
    }

    @Override
    public VideoInfoVO frontInfo(Long id) {
        Video video = videoMapper.selectVideoById(id);
        if (ObjectUtils.isEmpty(video)){
            return null;
        }
        VideoInfoVO vo = new VideoInfoVO();
        BeanUtils.copyProperties(video,vo);
        Long enterpriseId = tokenService.getUserId();
//        Long enterpriseId = 1l;
        VideoListVO one = videoMapper.selectOne(id,enterpriseId);
        if (!ObjectUtils.isEmpty(one)){
            vo.setWatchDuration(one.getWatchDuration());
            vo.setWatchPercentage(one.getWatchPercentage());
        }else {
            vo.setWatchDuration(0l);
            vo.setWatchPercentage(0l);
        }
        return vo;
    }
}
